#!/bin/bash
###################################
# KDE NEON launcher specific part #
###################################

# Add paths for games
append_dir PATH "$SNAP/usr/games"
append_dir PATH "$SNAP_DESKTOP_RUNTIME/usr/games"

# Qt Libs
prepend_dir LD_LIBRARY_PATH "$SNAP_DESKTOP_RUNTIME/usr/lib/$ARCH/qt5/libs"

# Add QT_PLUGIN_PATH (Qt Modules).
append_dir QT_PLUGIN_PATH "$SNAP/usr/lib/$ARCH/qt5/plugins"
append_dir QT_PLUGIN_PATH "$SNAP/usr/lib/$ARCH"
append_dir QT_PLUGIN_PATH "$SNAP_DESKTOP_RUNTIME/usr/lib/$ARCH/qt5/plugins"
append_dir QT_PLUGIN_PATH "$SNAP_DESKTOP_RUNTIME/usr/lib/$ARCH/"
# And QML2_IMPORT_PATH (Qt Modules).
append_dir QML2_IMPORT_PATH "$SNAP/usr/lib/$ARCH/qt5/qml"
append_dir QML2_IMPORT_PATH "$SNAP/lib/$ARCH"
append_dir QML2_IMPORT_PATH "$SNAP_DESKTOP_RUNTIME/usr/lib/$ARCH/qt5/qml"
append_dir QML2_IMPORT_PATH "$SNAP_DESKTOP_RUNTIME/lib/$ARCH"

# Fix locating the QtWebEngineProcess executable
export QTWEBENGINEPROCESS_PATH="$SNAP_DESKTOP_RUNTIME/usr/lib/$ARCH/qt5/libexec/QtWebEngineProcess"

# Removes Qt warning: Could not find a location
# of the system Compose files
export QTCOMPOSE="$SNAP_DESKTOP_RUNTIME/usr/share/X11/locale"
export QT_XKB_CONFIG_ROOT="/usr/share/X11/xkb"

# KIO specific
# Directly fork slaves.
export KDE_FORK_SLAVES=1
# Path to KIO slaves.
export KF5_LIBEXEC_DIR="$SNAP_DESKTOP_RUNTIME/usr/lib/$ARCH/libexec/kf5"

# Add path to VLC plugins
export VLC_PLUGIN_PATH="$SNAP_DESKTOP_RUNTIME/usr/lib/x86_64-linux-gnu/vlc/plugins"

# Ensure QtChooser behaves.
export QTCHOOSER_NO_GLOBAL_DIR=1
export QT_SELECT=5
# qtchooser hardcodes reference paths, we'll need to rewrite them properly
ensure_dir_exists "$XDG_CONFIG_HOME/qtchooser"
echo "$SNAP/usr/lib/qt5/bin" > "$XDG_CONFIG_HOME/qtchooser/5.conf"
echo "$SNAP/usr/lib/$ARCH" >> "$XDG_CONFIG_HOME/qtchooser/5.conf"
echo "$SNAP/usr/lib/qt5/bin" > "$XDG_CONFIG_HOME/qtchooser/default.conf"
echo "$SNAP/usr/lib/$ARCH" >> "$XDG_CONFIG_HOME/qtchooser/default.conf"

# This relies on qtbase patch
# 0001-let-qlibraryinfo-fall-back-to-locate-qt.conf-via-XDG.patch
# to make QLibraryInfo look in XDG_* locations for qt.conf. The paths configured
# here are applied to everything that uses QLibraryInfo as final fallback and
# has no XDG_* fallback before that. Currently the most interesting offender
# is QtWebEngine which will not work unless the Data path is correctly set.
cat << EOF > "$XDG_CONFIG_HOME/qt.conf"
[Paths]
Data = $SNAP_DESKTOP_RUNTIME/usr/share/qt5/
Translations = $SNAP_DESKTOP_RUNTIME/usr/share/qt5/translations
EOF

if [ -e "$SNAP_DESKTOP_RUNTIME/usr/share/i18n" ]; then
    export I18NPATH="$SNAP_DESKTOP_RUNTIME/usr/share/i18n"
    locpath="$XDG_DATA_HOME/locale"
    ensure_dir_exists "$locpath"
    export LOCPATH="$locpath:/usr/lib/locale"
    LC_ALL=C.UTF-8 async_exec "$SNAP/snap/command-chain/locale-gen"
fi
